package cn.zhsding.questionbank.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.zhsding.questionbank.dao.IKaoShiRepository;
import cn.zhsding.questionbank.dao.IKstRepository;
import cn.zhsding.questionbank.entity.KaoShiEntiry;
import cn.zhsding.questionbank.entity.KsEntity;
import cn.zhsding.questionbank.entity.vo.KaoShiVo;
import cn.zhsding.questionbank.entity.vo.KstVo;
import cn.zhsding.questionbank.util.KsUtils;

@RestController
@RequestMapping("/ks")
public class KouSuanController {
    @Resource
    private IKaoShiRepository kaoshiRepository;
    @Resource
    private IKstRepository kstRepository;
    
    @GetMapping("/inittm")
    public KaoShiVo inittm(Integer tmnum, Integer tmtime) {
        KaoShiVo vo = new KaoShiVo();
        KaoShiEntiry kaoshi = new KaoShiEntiry();
        kaoshi.setNum(tmnum);
        kaoshi.setTimeMinth(tmtime);
        List<KsEntity> kss = new ArrayList<>();
        for (int i = 0; i < tmnum; i++) {
            kss.add(KsUtils.getKs());
        }
        List<KstVo> kss1 = new ArrayList<>();
        List<KsEntity> saveAll = kstRepository.saveAll(kss);
        for (KsEntity e : saveAll) {
            KstVo kstVo = new KstVo(e);
            kstVo.setSelect(KsUtils.randomSelect(kstVo.getDa()));
            kss1.add(kstVo);
        }
        vo.setKstms(kss1);
        vo.setKaoshi(kaoshiRepository.save(kaoshi));
        
        return vo;
    }
}
